//
//  RESTClient.h
//  iFlick
//
//  Created by 张 霄鹤 on 09-10-31.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <Foundation/NSURLRequest.h>
#import <Foundation/NSURLResponse.h>
#import <Foundation/NSURLConnection.h>

@protocol RESTClientDelegate;



/**
 * REST 客户端，包括请求，返回相应，请求状态处理等
 */
@interface RESTClient : NSObject {
	NSString *serverAPI;
	NSString *secretKey;
	NSURLConnection *conn;
	id<RESTClientDelegate> _delegate;
}

@property(nonatomic, retain) id<RESTClientDelegate> delegate;

/**
 * 初始化，并且加入服务器地址
 */
- (id)initWithAPIURL:(NSString *)url withSecret:(NSString *)sec;

/**
 * 请求，并且等待返回结果，其中的所有参数都已经设置好了，包括 method, api_sig
 */
- (id)request:(NSString *)method withParameters:(NSDictionary *)params;

- (NSString *)genApiSig:(NSString *)method params:(NSDictionary *)paramDict;


//- (NSArray *)getSortedKeys:(NSDictionary *)params;


// NSURLConnection Delegate
/**
 * 连接出错的情况
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

/**
 * 接受数据完毕
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

/**
 * 接受响应
 */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

/**
 * 数据发送部分
 */
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten
	totalBytesWritten:(NSInteger)totalBytesWritten
	totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;

/**
 * 数据装载完毕
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

@end

@protocol RESTClientDelegate

/**
 * 数据加载完毕，处理数据
 */
- (void)didFinishLoad:(NSData *)data;

/**
 * 连接请求等发生错误
 */
- (void)didFail:(NSError *)error;

@end

